package com.cn.wanxi.servlet.back.facebook;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.FacebookModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.IFacebookService;
import com.cn.wanxi.service.impl.FacebookServiceImpl;
import com.cn.wanxi.util.DataEncoding;
import com.cn.wanxi.util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/back/facebook/info")
public class FacebookInfoServlet extends HttpServlet {
    IFacebookService facebookService = new FacebookServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //留言详情
        String fid = req.getParameter("id");
        int id = 0;
        if (!"".equals(fid))
            id = Integer.parseInt(fid);
        FacebookModel facebookModel = facebookService.getFacebook(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("facebook",facebookModel);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //留言分页列表和模糊查询
        String title = req.getParameter("title");
        String start_date = req.getParameter("start_date");
        String end_date = req.getParameter("end_date");
        String email = req.getParameter("email");
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        String sort = req.getParameter("sort");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        Map<String,Object> map = new HashMap<>();
        map.put("title",title);
        map.put("start_date",start_date);
        map.put("end_date",end_date);
        map.put("email",email);
        map.put("page",(page-1)*size);
        map.put("size",size);
        map.put("sort",sort);
        PageModel<FacebookModel> facebookList = facebookService.getFacebooks(map);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("facebook",facebookList);
        resp.getWriter().print(jsonObject);
    }
}
